import { Sequelize, DataTypes } from 'sequelize';
import initializeDataBase from '@src/db/db';

import UserModel from './User';

const FriendModel = async () => {
    const sequelize: Sequelize = await initializeDataBase();
    const Friend = sequelize.define('friend', {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        userId: {
            type: DataTypes.INTEGER,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        friendId: {
            type: DataTypes.INTEGER,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        message:{
            type: DataTypes.STRING(255),
            allowNull: true
        },
        status: {
            type: DataTypes.ENUM('pending', 'accepted','rejected'),
            allowNull: false,
            defaultValue: 'pending'
        }
    })

    const User = await UserModel();

    Friend.belongsTo(User, { foreignKey: 'userId' });
    Friend.belongsTo(User, { foreignKey: 'friendId' });
    
    await sequelize.sync({ alter: true }); // Sincroniza o modelo com o banco de dados
    return sequelize.models.friend;
}

export default FriendModel